﻿using System;
using System.Linq;
using HslCommunication.Core;
using HslCommunication.Core.Net;
using HslCommunication.Reflection;

namespace HslCommunication.Profinet.Omron
{
	/// <summary>
	/// 欧姆龙的Udp协议的实现类，地址类型和Fins-TCP一致，无连接的实现，可靠性不如<see cref="T:HslCommunication.Profinet.Omron.OmronFinsNet" /><br />
	/// Omron's Udp protocol implementation class, the address type is the same as Fins-TCP, 
	/// and the connectionless implementation is not as reliable as <see cref="T:HslCommunication.Profinet.Omron.OmronFinsNet" />
	/// </summary>
	/// <remarks>
	/// <inheritdoc cref="T:HslCommunication.Profinet.Omron.OmronFinsNet" path="remarks" />
	/// </remarks>
	// Token: 0x0200005D RID: 93
	public class OmronFinsUdp : NetworkUdpDeviceBase
	{
		/// <inheritdoc cref="M:HslCommunication.Profinet.Omron.OmronFinsNet.#ctor(System.String,System.Int32)" />
		// Token: 0x060007B2 RID: 1970 RVA: 0x0002976C File Offset: 0x0002796C
		public OmronFinsUdp(string ipAddress, int port)
		{
			base.WordLength = 1;
			this.IpAddress = ipAddress;
			this.Port = port;
			base.ByteTransform = new ReverseWordTransform();
			base.ByteTransform.DataFormat = DataFormat.CDAB;
			base.ByteTransform.IsStringReverseByteWord = true;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Omron.OmronFinsNet.#ctor" />
		// Token: 0x060007B3 RID: 1971 RVA: 0x00029804 File Offset: 0x00027A04
		public OmronFinsUdp()
		{
			base.WordLength = 1;
			base.ByteTransform = new ReverseWordTransform();
			base.ByteTransform.DataFormat = DataFormat.CDAB;
			base.ByteTransform.IsStringReverseByteWord = true;
		}

		/// <inheritdoc />
		// Token: 0x1700015E RID: 350
		// (get) Token: 0x060007B4 RID: 1972 RVA: 0x0002988C File Offset: 0x00027A8C
		// (set) Token: 0x060007B5 RID: 1973 RVA: 0x00029894 File Offset: 0x00027A94
		public override string IpAddress
		{
			get
			{
				return base.IpAddress;
			}
			set
			{
				base.IpAddress = value;
				this.DA1 = Convert.ToByte(base.IpAddress.Substring(base.IpAddress.LastIndexOf(".") + 1));
			}
		}

		/// <inheritdoc cref="P:HslCommunication.Profinet.Omron.OmronFinsNet.ICF" />
		// Token: 0x1700015F RID: 351
		// (get) Token: 0x060007B6 RID: 1974 RVA: 0x000298C8 File Offset: 0x00027AC8
		// (set) Token: 0x060007B7 RID: 1975 RVA: 0x000298D0 File Offset: 0x00027AD0
		public byte ICF { get; set; } = 128;

		/// <inheritdoc cref="P:HslCommunication.Profinet.Omron.OmronFinsNet.RSV" />
		// Token: 0x17000160 RID: 352
		// (get) Token: 0x060007B8 RID: 1976 RVA: 0x000298D9 File Offset: 0x00027AD9
		// (set) Token: 0x060007B9 RID: 1977 RVA: 0x000298E1 File Offset: 0x00027AE1
		public byte RSV { get; private set; } = 0;

		/// <inheritdoc cref="P:HslCommunication.Profinet.Omron.OmronFinsNet.GCT" />
		// Token: 0x17000161 RID: 353
		// (get) Token: 0x060007BA RID: 1978 RVA: 0x000298EA File Offset: 0x00027AEA
		// (set) Token: 0x060007BB RID: 1979 RVA: 0x000298F2 File Offset: 0x00027AF2
		public byte GCT { get; set; } = 2;

		/// <inheritdoc cref="P:HslCommunication.Profinet.Omron.OmronFinsNet.DNA" />
		// Token: 0x17000162 RID: 354
		// (get) Token: 0x060007BC RID: 1980 RVA: 0x000298FB File Offset: 0x00027AFB
		// (set) Token: 0x060007BD RID: 1981 RVA: 0x00029903 File Offset: 0x00027B03
		public byte DNA { get; set; } = 0;

		/// <inheritdoc cref="P:HslCommunication.Profinet.Omron.OmronFinsNet.DA1" />
		// Token: 0x17000163 RID: 355
		// (get) Token: 0x060007BE RID: 1982 RVA: 0x0002990C File Offset: 0x00027B0C
		// (set) Token: 0x060007BF RID: 1983 RVA: 0x00029914 File Offset: 0x00027B14
		public byte DA1 { get; set; } = 19;

		/// <inheritdoc cref="P:HslCommunication.Profinet.Omron.OmronFinsNet.DA2" />
		// Token: 0x17000164 RID: 356
		// (get) Token: 0x060007C0 RID: 1984 RVA: 0x0002991D File Offset: 0x00027B1D
		// (set) Token: 0x060007C1 RID: 1985 RVA: 0x00029925 File Offset: 0x00027B25
		public byte DA2 { get; set; } = 0;

		/// <inheritdoc cref="P:HslCommunication.Profinet.Omron.OmronFinsNet.SNA" />
		// Token: 0x17000165 RID: 357
		// (get) Token: 0x060007C2 RID: 1986 RVA: 0x0002992E File Offset: 0x00027B2E
		// (set) Token: 0x060007C3 RID: 1987 RVA: 0x00029936 File Offset: 0x00027B36
		public byte SNA { get; set; } = 0;

		/// <inheritdoc cref="P:HslCommunication.Profinet.Omron.OmronFinsNet.SA1" />
		// Token: 0x17000166 RID: 358
		// (get) Token: 0x060007C4 RID: 1988 RVA: 0x0002993F File Offset: 0x00027B3F
		// (set) Token: 0x060007C5 RID: 1989 RVA: 0x00029947 File Offset: 0x00027B47
		public byte SA1 { get; set; } = 13;

		/// <inheritdoc cref="P:HslCommunication.Profinet.Omron.OmronFinsNet.SA2" />
		// Token: 0x17000167 RID: 359
		// (get) Token: 0x060007C6 RID: 1990 RVA: 0x00029950 File Offset: 0x00027B50
		// (set) Token: 0x060007C7 RID: 1991 RVA: 0x00029958 File Offset: 0x00027B58
		public byte SA2 { get; set; }

		/// <inheritdoc cref="P:HslCommunication.Profinet.Omron.OmronFinsNet.SID" />
		// Token: 0x17000168 RID: 360
		// (get) Token: 0x060007C8 RID: 1992 RVA: 0x00029961 File Offset: 0x00027B61
		// (set) Token: 0x060007C9 RID: 1993 RVA: 0x00029969 File Offset: 0x00027B69
		public byte SID { get; set; } = 0;

		/// <inheritdoc cref="M:HslCommunication.Profinet.Omron.OmronFinsNet.PackCommand(System.Byte[])" />
		// Token: 0x060007CA RID: 1994 RVA: 0x00029974 File Offset: 0x00027B74
		private byte[] PackCommand(byte[] cmd)
		{
			byte[] array = new byte[10 + cmd.Length];
			array[0] = this.ICF;
			array[1] = this.RSV;
			array[2] = this.GCT;
			array[3] = this.DNA;
			array[4] = this.DA1;
			array[5] = this.DA2;
			array[6] = this.SNA;
			array[7] = this.SA1;
			array[8] = this.SA2;
			array[9] = this.SID;
			cmd.CopyTo(array, 10);
			return array;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Omron.OmronFinsNet.BuildReadCommand(System.String,System.UInt16,System.Boolean)" />
		// Token: 0x060007CB RID: 1995 RVA: 0x000299F8 File Offset: 0x00027BF8
		public OperateResult<byte[]> BuildReadCommand(string address, ushort length, bool isBit)
		{
			OperateResult<byte[]> operateResult = OmronFinsNetHelper.BuildReadCommand(address, length, isBit);
			bool flag = !operateResult.IsSuccess;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				result = OperateResult.CreateSuccessResult<byte[]>(this.PackCommand(operateResult.Content));
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Omron.OmronFinsNet.BuildWriteCommand(System.String,System.Byte[],System.Boolean)" />
		// Token: 0x060007CC RID: 1996 RVA: 0x00029A38 File Offset: 0x00027C38
		public OperateResult<byte[]> BuildWriteCommand(string address, byte[] value, bool isBit)
		{
			OperateResult<byte[]> operateResult = OmronFinsNetHelper.BuildWriteWordCommand(address, value, isBit);
			bool flag = !operateResult.IsSuccess;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				result = OperateResult.CreateSuccessResult<byte[]>(this.PackCommand(operateResult.Content));
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Omron.OmronFinsNet.Read(System.String,System.UInt16)" />
		// Token: 0x060007CD RID: 1997 RVA: 0x00029A78 File Offset: 0x00027C78
		[HslMqttApi("ReadByteArray", "")]
		public override OperateResult<byte[]> Read(string address, ushort length)
		{
			OperateResult<byte[]> operateResult = this.BuildReadCommand(address, length, false);
			bool flag = !operateResult.IsSuccess;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<byte[]>(operateResult);
			}
			else
			{
				OperateResult<byte[]> operateResult2 = this.ReadFromCoreServer(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = OperateResult.CreateFailedResult<byte[]>(operateResult2);
				}
				else
				{
					OperateResult<byte[]> operateResult3 = OmronFinsNetHelper.UdpResponseValidAnalysis(operateResult2.Content, true);
					bool flag3 = !operateResult3.IsSuccess;
					if (flag3)
					{
						result = OperateResult.CreateFailedResult<byte[]>(operateResult3);
					}
					else
					{
						result = OperateResult.CreateSuccessResult<byte[]>(operateResult3.Content);
					}
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Omron.OmronFinsNet.Write(System.String,System.Byte[])" />
		// Token: 0x060007CE RID: 1998 RVA: 0x00029B04 File Offset: 0x00027D04
		[HslMqttApi("WriteByteArray", "")]
		public override OperateResult Write(string address, byte[] value)
		{
			OperateResult<byte[]> operateResult = this.BuildWriteCommand(address, value, false);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				OperateResult<byte[]> operateResult2 = this.ReadFromCoreServer(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = operateResult2;
				}
				else
				{
					OperateResult<byte[]> operateResult3 = OmronFinsNetHelper.UdpResponseValidAnalysis(operateResult2.Content, false);
					bool flag3 = !operateResult3.IsSuccess;
					if (flag3)
					{
						result = operateResult3;
					}
					else
					{
						result = OperateResult.CreateSuccessResult();
					}
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Omron.OmronFinsNet.ReadBool(System.String,System.UInt16)" />
		// Token: 0x060007CF RID: 1999 RVA: 0x00029B7C File Offset: 0x00027D7C
		[HslMqttApi("ReadBoolArray", "")]
		public override OperateResult<bool[]> ReadBool(string address, ushort length)
		{
			OperateResult<byte[]> operateResult = this.BuildReadCommand(address, length, true);
			bool flag = !operateResult.IsSuccess;
			OperateResult<bool[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<bool[]>(operateResult);
			}
			else
			{
				OperateResult<byte[]> operateResult2 = this.ReadFromCoreServer(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = OperateResult.CreateFailedResult<bool[]>(operateResult2);
				}
				else
				{
					OperateResult<byte[]> operateResult3 = OmronFinsNetHelper.UdpResponseValidAnalysis(operateResult2.Content, true);
					bool flag3 = !operateResult3.IsSuccess;
					if (flag3)
					{
						result = OperateResult.CreateFailedResult<bool[]>(operateResult3);
					}
					else
					{
						result = OperateResult.CreateSuccessResult<bool[]>(Enumerable.ToArray<bool>(Enumerable.Select<byte, bool>(operateResult3.Content, (byte m) => m != 0)));
					}
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Omron.OmronFinsNet.Write(System.String,System.Boolean[])" />
		// Token: 0x060007D0 RID: 2000 RVA: 0x00029C34 File Offset: 0x00027E34
		[HslMqttApi("WriteBoolArray", "")]
		public override OperateResult Write(string address, bool[] values)
		{
			OperateResult<byte[]> operateResult = this.BuildWriteCommand(address, Enumerable.ToArray<byte>(Enumerable.Select<bool, byte>(values, (bool m) => m ? 1 : 0)), true);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				OperateResult<byte[]> operateResult2 = this.ReadFromCoreServer(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = operateResult2;
				}
				else
				{
					OperateResult<byte[]> operateResult3 = OmronFinsNetHelper.UdpResponseValidAnalysis(operateResult2.Content, false);
					bool flag3 = !operateResult3.IsSuccess;
					if (flag3)
					{
						result = operateResult3;
					}
					else
					{
						result = OperateResult.CreateSuccessResult();
					}
				}
			}
			return result;
		}

		/// <inheritdoc />
		// Token: 0x060007D1 RID: 2001 RVA: 0x00029CD4 File Offset: 0x00027ED4
		public override string ToString()
		{
			return string.Format("OmronFinsUdp[{0}:{1}]", this.IpAddress, this.Port);
		}
	}
}
